<template>
	<view view :class="['vh100',{'dark':theme == 'dark'}]">
		<view class="pt20 plr20">
			<view class="flex alcenter mb20">
				<picker class="bd_input ptb10 plr10 tc flex1 radius4" :value="index" :range="coinList" @change="pickChange" range-key="currency_name">
					<view class="flex between"><text>{{$t('bind.cur_select')}}</text> <text v-if="coinList[index]">{{coinList[index].currency_name}}</text></view>
				</picker>
			</view>
			<view class="flex alcenter mb20">
				<text>{{$t('bind.addr')}}：</text>
				<input type="text" value="" v-model="address" class="flex1 plr15 h40 bdb1f" :placeholder="$t('bind.p_addr')" />
			</view>
			<view class="flex alcenter">
				<text>{{$t('login.code')}} ：</text>
				<view class="bdb1f flex alcenter flex1">
					<input type="text" value="" class="flex1 plr15 h40 " v-model="code" :placeholder="$t('login.p_vcode')" />
					<view class="ml10 plr10 white bgBlue ptb5 radius4" @tap="getCode">
						<text v-if="!hasSend">{{$t('login.r_send')}}</text>
						<text v-else>{{second}}s</text>
					</view>
				</view>
			</view>
			<view class="mt40 bgBlue radius4 ptb10 white ft14 tc mb10" @tap="bindAddress">{{$t('bind.bind')}}</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapActions,mapGetters} from 'vuex';
	import {myMixins} from '@/common/myMixins.js'
	export default{
		mixins:[myMixins],
		data(){
			return {
				index:0,
				coinList:[],
				uerid:'',
				hasSend:false,
				timeInterval:'',
				address:'',
				code:'',
				second:60,
				area_code:"+86",
				accountNumber:""
			}
		},
		computed:{
			...mapState({
			  theme:state=>state.theme
			}),
		},
		onLoad() {
			this.getCoinList();
			uni.setNavigationBarTitle({
				title:this.$t('bind').bindAddr
			})
			this.$utils.setTheme(this.theme);
		},
		methods: {
			pickChange(e) {
				console.log(e);
				this.index=e.target.value;
				this.address=''
				this.getAddress()
			},
			getCoinList(){
				this.$utils.initDataToken({url:'wallet/list',type:'POST'},res=>{
					this.coinList=res.change_wallet.balance;
					this.getUserInfo();
				})
			},
			getUserInfo(){
				this.$utils.initDataToken({url:'user/info'},res=>{
					this.accountNumber = res.account_number;
					this.uerid=res.id;
					this.getAddress();
				})
			},
			getAddress(){
				this.$utils.getAddressOnline(
					{
						url:'walletMiddle/GetDrawAddress',
						data:{
							user_id:this.uerid,
							coin_name:this.coinList[this.index].currency_name,
							contract_address:this.coinList[this.index].contract_address?this.coinList[this.index].contract_address:0,
						},
					},
					res=>{
						console.log(res);
						if(res.code==0){
							this.address=res.data.address;
						}
					}
				)
			},
			// 获取验证码
			getCode(){
				// if(this.timeInterval) return;
				// this.$utils.getAddressOnline({url:'walletMiddle/SendVerificationcode',data:{user_id:this.uerid}},(res)=>{
				// 	console.log(res);
				// 	if(res.code==0){
				// 		this.$utils.showLayer(this.$t('bind.sendSuccess'));
				// 		this.hasSend=true;
				// 		this.timeInterval=setInterval(()=>{
				// 			if(this.second>=1){
				// 				this.second--;
				// 			}else{
				// 				this.hasSend=false
				// 				clearInterval(this.timeInterval);
				// 			}
				// 		},1000)
				// 	}else{
				// 		this.$utils.showLayer(res.errorinfo);
				// 	}
				// })
				let that = this;
				var url= 'sms_send';
				console.log(this.accountNumber)
				this.$utils.initDataToken({url:url,data:{user_string:this.accountNumber,scene:'band'},type:'post'},(res,msg)=>{
				   console.log(res)
				   console.log(msg)
				   setTimeout(()=>{
					 this.$utils.showLayer(msg)  
				   },100)
				   
				});
			},
			bindAddress(){
				if(!this.address){
					return this.$utils.showLayer(this.$t('bind.p_addr'))
				}
				if(!this.code){
					return this.$utils.showLayer(this.$t('login.p_vcode'))
				}
				
				var obj = {
					user_id:this.uerid,
					address:this.address,
					coin_name:this.coinList[this.index].currency_name,
					contract_address:this.coinList[this.index].contract_address,
					verificationcode:this.code,
					t:Date.parse(new Date())/1000,
				};
				// var obj = {
				// 	user_id:2,
				// 	address:'BANG btc',
				// 	coin_name:'BTC',
				// 	contract_address:'',
				// 	verificationcode:'738646',
				// 	t:1566373963,
				// };
				// var obj_str = JSON.stringify(obj);
				// console.log(obj_str);
				// console.log(this.$MD5(obj_str))
				// // console.log(this.$MD5('{"user_id":2,"address":"BANG btc","coin_name":"BTC","contract_address":"","verificationcode":"738646","t":1566373963}'))
				// var sign=this.$MD5(obj_str+'abcd4321');
				// console.log(sign);
				// this.$utils.getAddressOnline({url:'walletMiddle/BindDrawAddress',data:{data:obj_str,sign},type:'POST'},(res)=>{
				// 	console.log(res);
				// 	if(res.code==0){
				// 		// 成功
				// 		this.$utils.showLayer(this.$t('bind.bindOk'));
				// 	}else{
				// 		this.$utils.showLayer(res.errorinfo)
				// 	}
				// })
				this.$utils.getAddressOnline({
					url:'api/user/insert',
					data:{
						// data:obj_str,
						user_id:this.uerid,
						address:this.address,
						coin_name:this.coinList[this.index].currency_name,
						contract_address:this.coinList[this.index].contract_address?this.coinList[this.index].contract_address:0,
						verificationcode:this.code,
						t:Date.parse(new Date())/1000,
					},
					type:'POST',
				},(res)=>{
					console.log(res);
					if(res.type=='ok'){
						// 成功
						setTimeout(()=>{
							this.$utils.showLayer(this.$t('bind.bindOk'));
						},100)
						
					}else{
						setTimeout(()=>{
							this.$utils.showLayer(res.message)
						},100)
						
					}
				})
			}
		},
	}
</script>

<style>
</style>
